
//  ZXMyController.m
//  qianke
//
//  Created by ZX on 2017/6/29.
//  Copyright © 2017年 ZX. All rights reserved.
//

#import "ZXMyController.h"
#import "AddSaleActivityViewController.h"
#import "ZXshopLeaderCell.h"
#import "ZXAddSaleActivityController.h"
#import "ZXsaleChanceVIewController.h"
#import "ZXchanceToOrderController.h"
#import "choseRoleController.h"
@interface ZXMyController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *chanceNumLabelNext;
@property (weak, nonatomic) IBOutlet UILabel *chanceNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *chanceToOrderNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *chanceToOrderNumLabelLast;
@property (weak, nonatomic) IBOutlet UITableView *shopLeaderTableView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UIView *noshopLeaderView;
@property (weak, nonatomic) IBOutlet UILabel *helloLabel;
/*beginTime*/
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property(copy ,nonatomic)NSString*beginTime;
/*beginTime*/
@property(copy ,nonatomic)NSString*endTime;
/*beginTime*/
@property(copy ,nonatomic)NSString*thisBeginTime;
/*beginTime*/
@property(copy ,nonatomic)NSString*thisEndTime;


/*testarray*/
@property(copy ,nonatomic)NSArray*testArr;
@end

@implementation ZXMyController
@synthesize shopLeaderTableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addCustomer2) name:addcustomer2 object:nil];
    
    [self layOutSubviews];
    [self creatTableView];
    [self upload];
    [self searchCurrentMonthCount];
    [self searchLastMonthChanceCount];
    self.testArr = @[@"提车报表",@"发车报表",@"销售数据"];
}
//是否可以旋转
- (BOOL)shouldAutorotate
{
    return NO;
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(void)upload{
    UISwipeGestureRecognizer*recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.view addGestureRecognizer:recognizer];
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        [self searchCurrentMonthCount];
        [self searchLastMonthChanceCount];
        NSLog(@"swipe down");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"swipe up");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"swipe left");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"swipe right");
    }
}

-(void)searchCurrentMonthCount{
    NSString * beginTim = [ZXToolObj getMonthBeginWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getLastMonth]]];
    NSString * endTim = [ZXToolObj getMonthEndWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getLastMonth]]];
    NSString * thisbeginTim = [ZXToolObj getMonthBeginWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getMonth]]];
    NSString * thisendTim = [ZXToolObj getMonthEndWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getMonth]]];

    NSString *strURL = [NSString stringWithFormat:@"%@scrmForApp/ideaGuest/getCounts?userName=%@&uid=%@&currentMouthStartTime=%@&currentMouthEndTime=%@&lastMouthStartTime=%@&lastMouthEndTime=%@",ZXBaseURL,my_AppDelegate.userName,my_AppDelegate.uid,thisbeginTim,thisendTim,beginTim,endTim];

    NSLog(@"strUrl = %@",strURL);
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:my_AppDelegate.access_token forHTTPHeaderField:accessTokenKey];
    request = [mutableRequest copy];
    [HTTPHelper GetHeadmutableRequest:request success:^(NSDictionary *data) {
        NSLog(@"%@",data);
        NSDictionary * dictionary = [data objectForKey:@"data"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (dictionary) {
                _chanceNumLabel.text = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"currentMouthIdeaGuestCounts"]];
                _chanceNumLabelNext.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"lastMouthIdeaGuestCounts"]];
                if ([_chanceNumLabel.text isEqual:[NSNull null]]||!_chanceNumLabel.text) {
                    _chanceNumLabel.text = @"0";
                }
                if ([_chanceNumLabelNext.text isEqual:[NSNull null]]) {
                    _chanceNumLabelNext.text = @"0";
                }
            }else{
                _chanceNumLabel.text = @"0";
           
                _chanceNumLabelNext.text = @"0";
            }

        });
        NSLog(@"%@",[data objectForKey:@"message"]);
    } failure:^(NSError *error) {
        
    }];
}
-(void)searchLastMonthChanceCount{
    NSString * beginTim = [ZXToolObj getMonthBeginWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getLastMonth]]];
    NSString * endTim = [ZXToolObj getMonthEndWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getLastMonth]]];
    NSString * thisbeginTim = [ZXToolObj getMonthBeginWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getMonth]]];
    NSString * thisendTim = [ZXToolObj getMonthEndWith:[NSString stringWithFormat:@"2017-%@",[ZXToolObj getMonth]]];

    NSString * strURL = [NSString stringWithFormat:@"%@scrmForApp/guestOrder/getCounts?userName=%@&uid=%@&&currentMouthStartTime=%@&currentMouthEndTime=%@&lastMouthStartTime=%@&lastMouthEndTime=%@",ZXBaseURL,my_AppDelegate.userName,my_AppDelegate.uid,thisbeginTim,thisendTim,beginTim,endTim];
    NSLog(@"strUrl = %@",strURL);
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:my_AppDelegate.access_token forHTTPHeaderField:accessTokenKey];
    request = [mutableRequest copy];
    [HTTPHelper GetHeadmutableRequest:request success:^(NSDictionary *data) {
        NSLog(@"%@",data);
        NSDictionary * dictionary = [data objectForKey:@"data"];
        dispatch_async(dispatch_get_main_queue(), ^{

            if (dictionary) {
                _chanceToOrderNumLabel.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"currentMouthIdeaGuestToSaleCounts"]];
                _chanceToOrderNumLabelLast.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"lastMouthIdeaGuestToSaleCounts"]];
            }else{
                _chanceToOrderNumLabel.text = @"0";
                _chanceToOrderNumLabelLast.text = @"0";            }
        });
        NSLog(@"%@",[data objectForKey:@"message"]);
    } failure:^(NSError *error) {
        
    }];

}
#pragma mark --tableView
-(void)creatTableView{
    shopLeaderTableView.backgroundColor = [UIColor clearColor];
    shopLeaderTableView.separatorStyle = NO;
    [shopLeaderTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZXshopLeaderCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _testArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZXshopLeaderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"姓名";
        cell.todayLabel.text = @"本日提车销售";
        cell.month.text = @"本月提车销售";
        cell.todayFacehLabel.text = @"本日发车销售";
        cell.monthTicheLabel.text = @"本月发车销售";
    }else cell.titleLabel.text = _testArr[indexPath.row-1];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
#pragma mark --subViews
- (void)layOutSubviews{
    NSDate *nowDate = [NSDate date];
    
    NSString *week = [ZXToolObj weekdayStringFromDate:nowDate];
    
    NSString *strDate = [ZXToolObj getDateFromDay:nowDate];
    
    _timeLabel.text = [NSString stringWithFormat:@"%@  %@",strDate ,week];
    _helloLabel.text = [NSString stringWithFormat:@"Hello!%@",my_AppDelegate.nickName];
    _headImageView.layer.cornerRadius = _headImageView.frame.size.width/2;
    _headImageView.clipsToBounds = YES;
    // 设置viewController左上角的按钮
    UIButton *changeRolebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeRolebutton setTitle:@"切换角色" forState:UIControlStateNormal];
    changeRolebutton.frame = CGRectMake(0, ZXBigMargin+ZXMargin, 80, 44);

    changeRolebutton.titleLabel.font = [UIFont systemFontOfSize:13];
    [changeRolebutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeRolebutton addTarget:self action:@selector(changeRole) forControlEvents:UIControlEventTouchUpInside];
    [changeRolebutton sizeToFit];
    // 相当于让按钮的内容往左边偏移20
    changeRolebutton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    changeRolebutton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//    [self.view addSubview:changeRolebutton];
    // 设置viewController右上角的按钮
    UIButton *settingbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingbutton setTitle:@"切换角色" forState:UIControlStateNormal];
    settingbutton.frame = CGRectMake(ScreenWidth-64,ZXBigMargin+ZXMargin, 80, 44);
    
    settingbutton.titleLabel.font = [UIFont systemFontOfSize:13];
    [settingbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [settingbutton addTarget:self action:@selector(changeRole) forControlEvents:UIControlEventTouchUpInside];
    [settingbutton sizeToFit];
    // 相当于让按钮的内容往左边偏移20
    settingbutton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    settingbutton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//    [self.view addSubview:settingbutton];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [self searchCurrentMonthCount];
    [self searchLastMonthChanceCount];
//    [self getRolesInfo];
//    [self searchCurrentMonthAndLastMonthChanceCount];

}
-(void)viewDidDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark --btn_click
-(void)addCustomer2{
    ZXAddSaleActivityController *publish = [[ZXAddSaleActivityController alloc] init];
    [self.navigationController pushViewController:publish animated:YES];
    NSLog(@"添加客户");
    
}
-(void)changeRole{
    
}
- (IBAction)changeRole:(id)sender {
    [self.tabBarController setSelectedIndex:0];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ToChouseRole" object:nil];
}
- (IBAction)settingBtn_click:(id)sender {
    [self popoverPresentationController];
    NSLog(@"设置");
    
}
- (IBAction)add_clack:(id)sender {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"wodeaddsaleactivity" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)saleChanceBtnClick:(UIButton*)sender {
    ZXsaleChanceVIewController * vc = [ZXsaleChanceVIewController new];
    vc.title = @"销售机会";
    if (sender.tag == 11001) {
        vc.type = YES;
    }else {
        vc.type = NO;
    }
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)chanceToOrderBtnClick:(UIButton*)sender {
    ZXchanceToOrderController * vc = [ZXchanceToOrderController new];
    vc.title = @"销售订单";
    if (sender.tag == 11002) {
        vc.type = YES;
    }else {
    vc.type = NO;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
